package org.spindle.soasuite.controllers;

import org.spindle.soasuite.core.api.AExecutable;
import org.spindle.soasuite.core.api.annotations.Executable;
import org.spindle.soasuite.core.api.annotations.Parameter;
import org.spindle.soasuite.core.constants.StateConstants;
import org.spindle.soasuite.data.internal.User;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Executable(path = "/login")
class LoginController extends AExecutable {

    @Parameter(name = "u")
    private String username;

    @Parameter(name = "p")
    private String password;


    public Object execute() {
        Map<String, Object> auth = new HashMap<String, Object>(2);

        User user = getInternalDatasource().getUserDAO().getUser(username, password);

        if (user != null) {
            auth.put(StateConstants.AUTH, true);
            auth.put(StateConstants.USER_ID, user.getId());

            setStateObject(StateConstants.AUTH, true);
            setStateObject(StateConstants.USER_ID, user.getId());
            setStateObject(StateConstants.USER, user);

        } else {
            setStateObject(StateConstants.AUTH, false);

            auth.put(StateConstants.AUTH, false);
            auth.put("errorMessage", "Sorry buddy! Go home!");
        }

        return auth;
    }

    @Override
    public String getName() {
        return "LoginController";
    }
}

